<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">


      <el-form-item label="启用/禁用" prop="commissionState">
        <el-input
          v-model="queryParams.commissionState"
          placeholder="请输入启用/禁用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['supe:commission:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supe:commission:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['supe:commission:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supe:commission:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="commissionId" />
      <el-table-column label="总收益%" align="center" prop="totalRevenue" />
      <el-table-column label="超管佣金金额%" align="center" prop="commissionMoney" />
      <el-table-column label="分站佣金金额%" align="center" prop="substationMoney" />
      <el-table-column label="客户佣金金额%" align="center" prop="clientMoney" />
      <el-table-column label="地址" align="center" prop="commissionAddress" />

      <!--<el-table-column label="分级,暂用" align="center" prop="commissionGrading" />-->
      <el-table-column label="启用/禁用" align="center" prop="commissionState" >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.commissionState === '0' ? 'success' : 'danger'"
            disable-transitions>{{scope.row.commissionState ==='0' ? '启动' : '禁用'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supe:commission:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['supe:commission:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改超管分销佣金对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="总收益" prop="totalRevenue">
          <el-input v-model="form.totalRevenue" placeholder="请输入总收益" />
        </el-form-item>

        <el-form-item label="平台佣金比例" prop="commissionMoney">
          <el-input v-model="form.commissionMoney" placeholder="请输入佣金金额" >
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <!--<el-form-item label="分级,暂用" prop="commissionGrading">-->
          <!--<el-input v-model="form.commissionGrading" placeholder="请输入分级,暂用" />-->
        <!--</el-form-item>-->
        <el-form-item label="启用/禁用" prop="commissionState">
          <el-select v-model="form.commissionState" @change="getList" id="commissionState">
            <el-option value="0" label="启用"></el-option>
            <el-option value="1" label="禁用"></el-option>
          </el-select>
          <!--<el-input v-model="form.commissionState" placeholder="请输入0启用  1禁用" />-->
        </el-form-item>

        <el-form-item label="分站佣金比例" prop="substationMoney">
          <el-input v-model="form.substationMoney" placeholder="请输入分站佣金金额" >
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="服务站佣金比例" prop="clientMoney">
          <el-input v-model="form.clientMoney" placeholder="请输入客户佣金金额" >
            <template slot="append">%</template>
          </el-input>

        </el-form-item>
        <el-form-item label="地址" prop="commissionAddress">
          <el-input v-model="form.commissionAddress" placeholder="请输入地址" />
        </el-form-item>

        <!--<el-form-item label="最后修改时间" prop="modifiedTime">-->
          <!--<el-date-picker clearable size="small" style="width: 200px"-->
                          <!--v-model="form.modifiedTime"-->
                          <!--type="date"-->
                          <!--value-format="yyyy-MM-dd"-->
                          <!--placeholder="选择最后修改时间">-->
          <!--</el-date-picker>-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCommission, getCommission, delCommission, addCommission, updateCommission, exportCommission  } from "@/api/super/commission";

  export default {
        name: "commission",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 超管分销佣金表格数据
        commissionList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          commissionMoney: null,
          commissionGrading: null,
          commissionState: null,
          modifiedTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          commissionMoney: [
            { required: true, message: "佣金金额不能为空", trigger: "blur" }
          ],
          commissionState: [
            { required: true, message: "0启用  1禁用不能为空", trigger: "blur" }
          ],
          modifiedTime: [
            { required: true, message: "最后修改时间不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询超管分销佣金列表 */
      getList() {
        this.loading = true;
        listCommission(this.queryParams).then(response => {
          this.commissionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          commissionId: null,
          commissionMoney: null,
          commissionGrading: null,
          commissionState: null,
          modifiedTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.commissionId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "设置分销佣金比例";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const commissionId = row.commissionId || this.ids
        getCommission(commissionId).then(response => {
          this.form = response.data;
        this.open = true;
        this.title = "修改设置分销佣金比例";
      });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.commissionId != null) {
              updateCommission(this.form).then(response => {
                if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
            } else {
              addCommission(this.form).then(response => {
                if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const commissionIds = row.commissionId || this.ids;
        this.$confirm('是否确认删除超管分销佣金编号为"' + commissionIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCommission(commissionIds);
        }).then(() => {
          this.getList();
        this.msgSuccess("删除成功");
      }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有超管分销佣金数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCommission(queryParams);
        }).then(response => {
          this.download(response.msg);
      }).catch(function() {});
      }
    }

    }
</script>

<style scoped>

</style>
